{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:game type='manage' /}
        </div>
    </div>
    <div class="layui-inline">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
        <!--<button class="layui-btn my-btn" type="button" id="add">添加</button>-->
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="tableBar">
</script>
{/block}

{block name="css"}
<style>
</style>
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var jsonObj = {
                        "game_id": $('#game_id').val()
                        ,"publish_attr": $('#publish_attr').val()
                        ,"plat_attr": $('#plat_attr').val()
                        ,"game_attr": $('#game_attr').val()
                        ,"game_tags": $('#game_tags').val()
                    };

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: jsonObj
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("GameServer/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function () {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'game_id', title: '游戏ID', width: 100, sort:true}
                ,{field:'game_name', title: '游戏名称', width: 200}
                ,{field:'server_id', title: '区服ID', width: 100}
                ,{field:'server_name', title: '区服名称', width: 200}
                ,{field:'open_time', title: '开服时间', width: 200}
                ,{field:'status', title: '状态', width: 100}
//                ,{title: '操作', align:'center', width: 300, toolbar:'#tableBar'}
            ]]
        });

        //点击游戏标签，出现标签选择
        $('#game_tags').click(function(){
            if( $("#game_tags_box").attr("switch") == "off" ){
                $("#game_tags_box").fadeIn(200);
                $("#game_tags_box").attr("switch","on");
            }else{
                $("#game_tags_box").fadeOut(200);
                $("#game_tags_box").attr("switch","off");
            }
        })
        //点击标签，加入样式
        $('.game_tags_btn').click(function(){
            if( $(this).attr('switch') == 'off' ){
                var val = $('#game_tags').val();
                val = val=='' ? $(this).text() : val + ',' + $(this).text();
                $('#game_tags').val(val);
                $(this).addClass('my-btn');
                $(this).attr('switch', 'on');
            }else{
                var val = ',' + $('#game_tags').val();
                val = val.replace(',' + $(this).text(), "");
                $('#game_tags').val(val.substr(1,val.length));
                $(this).removeClass('my-btn');
                $(this).attr('switch', 'off');
            }
            return false;
        });
        //点击文档，隐藏标签
        $(document).bind("click",function(e){
            var target  = $(e.target);
            if(target.closest("#game_tags_box,#game_tags").length == 0){
                $("#game_tags_box").fadeOut(200);
                $("#game_tags_box").attr("switch","off");
            };
            e.stopPropagation();
        });

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        //监听搜索栏
        $('#add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['100%', '100%']
                ,content: '{:url("GameList/add")}'
            });
        })

        //修改状态
        form.on('switch(status)', function(obj){
            var status = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            $.post('{:url("GameList/edit")}',{game_id:obj.value, status:status}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });


        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data;

            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("GameList/edit")}?game_id='+data.game_id
                    ,cancel: function(){
                        $.post('{:url("GameList/index")}',{game_id: data.game_id}, function(json){
                            obj.update({
                                game_id: json.data[0].game_id
                                ,game_name: json.data[0].game_name
                                ,charge_rate: json.data[0].charge_rate
                                ,publish_attr: json.data[0].publish_attr
                                ,plat_attr: json.data[0].plat_attr
                                ,game_attr: json.data[0].game_attr
                                ,game_tags: json.data[0].game_tags
                                ,add_time: json.data[0].add_time
                                ,status: json.data[0].status
                                ,online_time_region: json.data[0].online_time_region
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("GameList/del")}',{game_id: data.game_id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }else if(obj.event === 'params'){
                layer.open({
                    type: 1
                    ,title: '参数'
                    ,area: ['800px', '300px']
                    ,shadeClose: true
                    ,offset: '200px'
                    ,shade: 0
                    ,content: '<div style="padding: 20px 40px; font-size: 16px;"><p style="line-height:25px;">游戏名称： '+data.game_name+'</p><p style="line-height:25px;">游戏id(tz_gameid)： '+data.game_id+'</p><p style="line-height:25px;">渠道id(tz_channelid)： 1000</p><p style="line-height:25px;">秘钥(secret)： '+data.secret+'</p><p style="line-height:25px;">module_id： '+data.game_id+'</p></div>'
                });
            }else if(obj.event === 'upload'){
                layer.open({
                    type: 2
                    ,title: '上传母包'
                    ,offset: '100px'
                    ,resize: true
                    ,area: ['1000px', '650px']
                    ,content: '{:url("GameList/upload")}?game_id='+data.game_id
                });
            }
        });
    });
</script>
{/block}